package indi.flynn.singleton;

/**
 * 巧克力工厂：只有当锅炉填满原料并加热后才能生产出巧克力
 * 工厂只有一口锅炉
 *
 * @author liufenglin
 * @date 2018年2月27日
 */
public class ChocolateFactory {
	/** 锅炉是否为空 */
	private boolean empty;
	
	/** 锅炉是否加热 */
	private boolean boiled;
	
	public volatile static ChocolateFactory uniqueInstance = null;

	private ChocolateFactory() {
		empty = true;
		boiled = false;
	}

	public static ChocolateFactory getInstance() {

		if (uniqueInstance == null) {
			synchronized (ChocolateFactory.class) {
				if (uniqueInstance == null) {
					uniqueInstance = new ChocolateFactory();
				}
			}
		}
		return uniqueInstance;
	}

	public void fill() {
		if (empty) {
			// 添加巧克力原料
			empty = false;
			boiled = false;
		}
	}

	public void boil() {
		if ((!empty) && (!boiled)) {
			// 加热
			boiled = true;
		}
	}
	
	public void drain() {
		if ((!empty) && boiled) {
			// 导出生产好的巧克力
			empty = true;
		}
	}
}
